# -*- coding: UTF-8 -*-
# cython: language_level=3
import re
import socket

import requests
from lxml.html import fromstring
from requests.utils import get_encodings_from_content

from base.base_parser import get_all_url
from utils.own_tools import fake_ua, get_host, raise_error_site_name


def get_website_info(home_page, host=''):
    title = ''
    ip = ''
    icp = ''
    video_license = ''
    # visit_status = 0

    fake_headers = {'User-Agent': fake_ua.random, 'Content-Type': 'text/html;charset=UTF-8'}

    try:
        response = requests.get(home_page, headers=fake_headers, timeout=(5, 10), stream=True, verify=False)
        title = response.status_code
        response.raise_for_status()
        response_url = response.url
        if response_url:
            _host = get_host(response_url)
            if _host['home_page'] and _host['home_page'] != home_page:
                home_page = _host['home_page']
                host = _host['host']
            # print(response_url)

        if 'text/html' not in response.headers['Content-Type'].lower() or (
                'Content-Length' in response.headers and int(response.headers['Content-Length']) > 5000000):
            print(response.headers['Content-Type'], response.headers['Content-Length'])
            title = 'not_html'
            raise Exception('not html')

        try:
            ip = response.raw._connection.sock.getpeername()[0]
        except:
            pass

        if not response.encoding or response.encoding == 'ISO-8859-1':
            encodings = get_encodings_from_content(response.text)
            encodings = encodings[0] if encodings else response.apparent_encoding

            if (not encodings) or (encodings.lower() not in ['gbk', 'utf-8', 'utf8', 'gb2312']):
                response.encoding = 'gbk'
            else:
                response.encoding = encodings
        html_str = response.text
        html = fromstring(html_str)

        title = html.xpath('//title/text()')
        title = title[0].strip() if title else ''
        if not title:
            title = re.search('document.title[\'"=\s]+(.*?)[\'"]+', html_str)
            title = title.group(1) if title else ''
        if not title:
            title = html.xpath('//meta[@name="Keywords" or @name="Description"]/text()')
            title = title[0].strip() if title else ''
        if not title:
            title = html.xpath('//*[@class="title"]/text()')
            title = [t.strip() for t in title if t.strip()]
            title = title[0].strip() if title else ''

        icp = re.search('([\u4e00-\u9fa5]\s*ICP\s*[\u4e00-\u9fa5]*\s*\d{4,12}\s*号*-*\d*)', html_str)
        icp = icp.group(1) if icp else ''

        video_license = re.search(
            '((信息网络传播视听节目许可证|信息网络传播视听许可证|视听许可证).{0,4}\d{0,12}号*)', html_str)
        video_license = video_license.group(1) if video_license else ''

    except Exception as e:
        print(e)
        visit_status = 1
    else:
        try:
            raise_error_site_name(title)
        except Exception as e:
            visit_status = 1
        else:
            visit_status = 0
        host and get_all_url(host, html)

    if not ip:
        try:
            ip = socket.gethostbyname(home_page.split('/')[2])
            assert ip
        except:
            pass
    return {"home_page": home_page, "host": host, "title": title, "ip": ip, "icp": icp, "video_license": video_license,
            "visit_status": visit_status}


if __name__ == "__main__":
    print(get_website_info('http://test.ustc.edu.cn/'))
